# -*- coding: utf-8 -*-
from redis import Redis
from json import loads
import aiohttp
import time
import asyncio

#连接数据库
#取出所在数据库中的ip
#访问httpbin网站，验证是否有效
#多久时间验证一次ip是否有效
#压缩回数据库中

class redis_clint_iplist(object):
    def __init__(self):
        self.redis_clint=Redis(host='localhost',port='6379',encoding='utf-8')
        self.ip_list_yco=[]
        self.http_url='http://httpbin.org/get'

    async def For_get_Html(self,urls):
        async with aiohttp.ClientSession()as session:
            print(urls)
            try:
                async with session.get(self.http_url,proxy=urls) as response:
                    page_text = await response.text()
                    print(page_text,'ok---------------')
            except Exception as e:
                print(e)
                pass


    def For_redis_list(self):
        cc=self.redis_clint.lrange('pro:items',0,10000)
        for xx in cc:
            x1 = loads(xx)
            ok_ip_urls='http://'+x1['ip']+':'+x1['prot']
            #print(ok_ip_urls)
            paks=self.For_get_Html(ok_ip_urls)
            tasks = asyncio.ensure_future(paks)
            self.ip_list_yco.append(tasks)
            loop = asyncio.get_event_loop()
            loop.run_until_complete(asyncio.wait(self.ip_list_yco))
            # self.For_get_Html(ok_ip_urls)

            #


aa=redis_clint_iplist()
aa.For_redis_list()


